package com.qsxc.enums.http;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 与前端进行特殊交互需要使用的状态码
 */
@Getter
@AllArgsConstructor
public enum CustomHttpStatus {
    /**
     * 客户端看到401状态码时，应该重新登陆
     */
    UNAUTHORIZED(401, "未授权"),

    PARAMERROR(1004, "参数错误"),
    ;

    private final int value;

    private final String msg;

    /**
     * Return a string representation of this status code.
     */
    @Override
    public String toString() {
        return this.value + " " + name();
    }


    public static CustomHttpStatus valueOf(int statusCode) {
        CustomHttpStatus status = resolve(statusCode);
        if (status == null) {
            throw new IllegalArgumentException("没有找到该Http状态码包含状态 [" + statusCode + "]");
        }
        return status;
    }

    public static CustomHttpStatus resolve(int statusCode) {
        for (CustomHttpStatus status : values()) {
            if (status.value == statusCode) {
                return status;
            }
        }
        return null;
    }


}
